﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;

namespace Common
{
    public class HttpRequestHelper
    {
        #region  POST请求，参数以XML形式传递
        /// <summary>
        /// 发起HTTP请求
        /// </summary>
        /// <param name="m_Doc"></param>
        /// <param name="m_QuestURL"></param>
        /// <returns></returns>
        public static string GetRequest(XmlDocument m_Doc, string m_QuestURL)
        {
            //Post请求地址
            HttpWebRequest m_Request = (HttpWebRequest)WebRequest.Create(m_QuestURL);
            //相应请求的参数
            byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(m_Doc.OuterXml);
            m_Request.Method = "Post";
            m_Request.ContentType = "application/x-www-form-urlencoded";
            m_Request.ContentLength = data.Length;
            m_Request.Timeout = 100000;
            //请求流
            Stream requestStream = m_Request.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();
            //响应流
            HttpWebResponse m_Response = (HttpWebResponse)m_Request.GetResponse();
            Stream responseStream = m_Response.GetResponseStream();
            StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
            //获取返回的信息
            string result = streamReader.ReadToEnd();
            streamReader.Close();
            responseStream.Close();
            return result;
        }
        #endregion

        #region  POST请求，参数以XML形式传递
        /// <summary>
        /// 发起HTTP请求
        /// </summary>
        /// <param name="m_Doc"></param>
        /// <param name="m_QuestURL"></param>
        /// <returns></returns>
        public static string GetRequest(string requestXml, string m_QuestURL, out string error)
        {
            string result = "";
            error = "";
            //Post请求地址
            try
            {
                HttpWebRequest m_Request = (HttpWebRequest)WebRequest.Create(m_QuestURL);
                //相应请求的参数
                byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(requestXml);
                m_Request.Method = "Post";
                m_Request.ContentType = "application/x-www-form-urlencoded";
                m_Request.ContentLength = data.Length;
                m_Request.Timeout = 100000;
                //请求流
                Stream requestStream = m_Request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                //响应流
                HttpWebResponse m_Response = (HttpWebResponse)m_Request.GetResponse();
                Stream responseStream = m_Response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                //获取返回的信息
                result = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
            }
            catch (Exception ex)
            {
                result = "";
                Logger.WriteLog(string.Format("请求接口异常,请求地址：{0}", m_QuestURL), ex);
                error = string.Format("请求接口异常,请求地址：{0}", m_QuestURL);
            }
            return result;
        }

        /// <summary>
        /// 发起HTTP请求,可传编码方式
        /// </summary>
        /// <param name="m_Doc"></param>
        /// <param name="m_QuestURL"></param>
        /// <returns></returns>
        public static string GetRequest(string requestXml, string m_QuestURL, string CodingType, out string error)
        {
            string result = "";
            error = "";
            //Post请求地址
            try
            {
                HttpWebRequest m_Request = (HttpWebRequest)WebRequest.Create(m_QuestURL);
                //相应请求的参数
                byte[] data = Encoding.GetEncoding(CodingType).GetBytes(requestXml);
                m_Request.Method = "Post";
                m_Request.ContentType = "application/x-www-form-urlencoded";
                m_Request.ContentLength = data.Length;
                m_Request.Timeout = 100000;
                //请求流
                Stream requestStream = m_Request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                //响应流
                HttpWebResponse m_Response = (HttpWebResponse)m_Request.GetResponse();
                Stream responseStream = m_Response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(CodingType));
                //获取返回的信息
                result = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
            }
            catch (Exception ex)
            {
                result = "";
                Logger.WriteLog(string.Format("请求接口异常,请求地址：{0}", m_QuestURL), ex);
                error = string.Format("请求接口异常,请求地址：{0}", m_QuestURL);
            }
            return result;
        }

        /// <summary>
        /// 发起HTTP请求
        /// </summary>
        /// <param name="m_Doc"></param>
        /// <param name="m_QuestURL"></param>
        /// <returns></returns>
        public static string GetRequest(string requestXml, string m_QuestURL)
        {
            string result = "";
            //Post请求地址
            try
            {
                HttpWebRequest m_Request = (HttpWebRequest)WebRequest.Create(m_QuestURL);
                //相应请求的参数
                byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(requestXml);
                m_Request.Method = "Post";
                m_Request.ContentType = "application/x-www-form-urlencoded";
                m_Request.ContentLength = data.Length;
                m_Request.Timeout = 100000;
                //请求流
                Stream requestStream = m_Request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                //响应流
                HttpWebResponse m_Response = (HttpWebResponse)m_Request.GetResponse();
                Stream responseStream = m_Response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                //获取返回的信息
                result = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
            }
            catch (Exception ex)
            {
                result = "";
                Logger.WriteLog(string.Format("请求接口异常,请求地址：{0}", m_QuestURL), ex);
            }
            return result;
        }

        public static string GetRequest(string requestXml, string m_QuestURL, string encoding)
        {
            string result = "";
            //Post请求地址
            try
            {
                HttpWebRequest m_Request = (HttpWebRequest)WebRequest.Create(m_QuestURL);
                //相应请求的参数
                byte[] data = Encoding.GetEncoding(encoding).GetBytes(requestXml);
                m_Request.Method = "Post";
                m_Request.ContentType = "application/x-www-form-urlencoded";
                m_Request.ContentLength = data.Length;
                m_Request.Timeout = 100000;
                //请求流
                Stream requestStream = m_Request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                //响应流
                HttpWebResponse m_Response = (HttpWebResponse)m_Request.GetResponse();
                Stream responseStream = m_Response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
                //获取返回的信息
                result = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
            }
            catch (WebException ex)
            {
                HttpWebResponse res = ex.Response as HttpWebResponse;
                if (res != null && res.StatusCode == HttpStatusCode.InternalServerError)
                {
                    Stream s = res.GetResponseStream();
                    if (s != null)
                    {
                        StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding("UTF-8"));
                        result = objReader.ReadToEnd();
                        objReader.Close();
                    }
                    else
                    {
                        result = ex.ToString();
                    }
                }
                else
                {
                    result = ex.ToString();
                }
                Logger.WriteLog(string.Format("请求接口异常,请求地址：{0},参数：{1}", m_QuestURL, requestXml), ex);
                return result;
            }
            catch (Exception ex)
            {
                result = ex.ToString();
                Logger.WriteLog(string.Format("请求接口异常,请求地址：{0}", m_QuestURL), ex);
            }
            return result;
        }

        /// <summary>
        /// POST请求，参数以XML形式传递
        /// </summary>
        /// <param name="requestXml">XML请求内容</param>
        /// <param name="m_QuestURL">请求地址</param>
        /// <param name="timeout">超时时间</param>
        /// <returns></returns>
        public static string GetRequest(string requestXml, string m_QuestURL, int timeout)
        {
            string result = "";
            //Post请求地址
            try
            {
                HttpWebRequest m_Request = (HttpWebRequest)WebRequest.Create(m_QuestURL);
                //相应请求的参数
                byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(requestXml);
                m_Request.Method = "Post";
                m_Request.ContentType = "application/x-www-form-urlencoded";
                m_Request.ContentLength = data.Length;
                m_Request.Timeout = timeout;
                //请求流
                Stream requestStream = m_Request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                //响应流
                HttpWebResponse m_Response = (HttpWebResponse)m_Request.GetResponse();
                Stream responseStream = m_Response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                //获取返回的信息
                result = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
            }
            catch (WebException timeEx)
            {
                result = timeEx.Status.ToString();
                Logger.WriteLog(string.Format("请求接口超时,请求地址：{0}", m_QuestURL), timeEx);
            }
            catch (Exception ex)
            {
                result = "error";
                Logger.WriteLog(string.Format("请求接口异常,请求地址：{0}", m_QuestURL), ex);
            }
            return result;
        }
        #region 不接收返回参数的Post请求
        /// <summary>
        /// 不接收返回参数的Post请求
        /// </summary>
        /// <param name="requestXml"></param>
        /// <param name="m_QuestURL"></param>
        public static void GetRequestNoResponse(string requestXml, string m_QuestURL)
        {
            try
            {
                HttpWebRequest m_Request = (HttpWebRequest)WebRequest.Create(m_QuestURL);
                //相应请求的参数
                byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(requestXml);
                m_Request.Method = "Post";
                m_Request.ContentType = "application/x-www-form-urlencoded";
                m_Request.ContentLength = data.Length;
                m_Request.Timeout = 100000;
                //请求流
                Stream requestStream = m_Request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
            }
            catch (Exception ex)
            {
                Logger.WriteLog(string.Format("请求接口异常,请求地址：{0}", m_QuestURL), ex);
            }
        }
        #endregion
        #endregion

        #region  GET请求
        public static string GetRequest(string m_QuestURL)
        {
            string result = string.Empty;
            HttpWebRequest request = null;
            StreamReader avReader = null;
            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create(m_QuestURL);
                //request.Proxy = NeedProxy.GetProxy();
                request.Timeout = 100000;
                avReader = new StreamReader(request.GetResponse().GetResponseStream());
                result = avReader.ReadToEnd();

            }
            catch (Exception err)
            {
                result = "Error:" + err.Message;
            }
            finally
            {
                if (avReader != null)
                {
                    request.Abort();
                    avReader.Close();
                }
            }
            return result;
        }

        public static string GetRequest(string m_QuestURL, Encoding encode)
        {
            string result = string.Empty;
            HttpWebRequest request = null;
            StreamReader avReader = null;
            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create(m_QuestURL);
                //request.Proxy = NeedProxy.GetProxy();
                request.Timeout = 100000;
                avReader = new StreamReader(request.GetResponse().GetResponseStream(), encode);
                result = avReader.ReadToEnd();

            }
            catch (Exception err)
            {
                result = "Error:" + err.Message;
            }
            finally
            {
                if (avReader != null)
                {
                    request.Abort();
                    avReader.Close();
                }
            }
            return result;
        }
        #endregion
    }
}
